package ipebg.mzk.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTool {

	SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	SimpleDateFormat qf = new SimpleDateFormat("yyyyMM");
	SimpleDateFormat yf = new SimpleDateFormat("yyyy");
    SimpleDateFormat mm = new SimpleDateFormat("MM");
	SimpleDateFormat ff = new SimpleDateFormat("E");
	
	static SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd");
	static SimpleDateFormat sf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	
	static SimpleDateFormat sf3 = new SimpleDateFormat("yyyyMMddHHmmss");
	
	static SimpleDateFormat sf4 = new SimpleDateFormat("yyyyMMdd");
	
	//獲取上個月時間
	public static String getBeforeMonth(SimpleDateFormat sf,String date){
		Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sf.parse(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        cal.add(Calendar.MONTH, -1);
        return sf.format(cal.getTime());
	}
	
	//獲取下個月時間
	public static String getAfterMonth(SimpleDateFormat sf,String date){
		Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sf.parse(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        cal.add(Calendar.MONTH, +1);
        return sf.format(cal.getTime());
	}
	
	public static String toShortDate(Date date1){
		return sf1.format(date1);
	}
	
	public static String toLongDate(Date date1){
		if (date1==null){
			return "";
		}else{
		   return sf2.format(date1);
		}
	}
	
	public static String toSerDate(Date date1){
		if (date1==null){
			return "";
		}else{
		   return sf3.format(date1);
		}
	}
	
	public static String getSysdate() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String sysdate_time = df.format(c.getTime());
		return sysdate_time;
	}
	
	public static String getCurrentTime(String fm) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat(fm);
		String sysdate_time = df.format(c.getTime());
		return sysdate_time;
	}	
	//把特定日期格式轉換成date類型
		public static Date stringToDateByFormat(String str,String formatStr) {

			Date rcreate = new Date();
			java.text.SimpleDateFormat newstr = new java.text.SimpleDateFormat(formatStr);

			try {
				rcreate = newstr.parse(str);
			} catch (ParseException e) {

				e.printStackTrace();
			}

			return rcreate;
		}
	//日期增加一天
	public Date addDay(Date date,int n){
		
		
		Calendar cd=Calendar.getInstance();
		cd.setTime(date);
		cd.add(Calendar.DATE, n);
		return cd.getTime();
		
	}
	
	public static String toIntDate(Date date1){
		return sf4.format(date1);
	}
	
	public static Date stringToDate(String str) {

		Date rcreate = new Date();
		java.text.SimpleDateFormat newstr = new java.text.SimpleDateFormat("dd/MM/yyyy");

		try {
			rcreate = newstr.parse(str);
		} catch (ParseException e) {

			e.printStackTrace();
		}

		return rcreate;

	}
	
	public static Integer getDaysOfMonth(SimpleDateFormat sf,String date) {
		Integer year,month, days=0;
		SimpleDateFormat sfy = new SimpleDateFormat("yyyy");
		SimpleDateFormat sfm = new SimpleDateFormat("MM");
		try {
			Date d = sf.parse(date);
			year = Integer.valueOf(sfy.format(d));
			month = Integer.valueOf(sfm.format(d));
			if(month!=2){
				switch(month){
					case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31 ;break;
					case 4:case 6:case 9:case 11:days = 30;
				}
			}else{
				if(year%4==0 && year%100!=0 || year%400==0) 
					days = 29;
				else 
					days = 28;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}
	
	public static void main(String[] args){
		
		System.out.println(DateTool.toLongDate(new Date()));
		
		   Calendar c = Calendar.getInstance();
		  // System.out.println( getSysdate());
		   System.out.println(c.get(Calendar.HOUR_OF_DAY) );
		   if(c.get(Calendar.HOUR_OF_DAY )==7){
			   System.out.println(Calendar.HOUR_OF_DAY );
		   }
		   /*
		   for (int i=0;i<10;i++){
		   System.out.println(String.valueOf(c.getTimeInMillis()));
		   }
		   System.out.println(c.getTime().toString());
           System.out.println(Integer.valueOf("001"));
           */
	}

}
